//
//  BoletoUtil.m
//  iBoleto
//
//  Created by Diego Fincatto on 13/01/11.
//  Copyright 2011 Wmix Distribuidora Ltda. All rights reserved.
//

#import "BoletoUtil.h"

@implementation BoletoUtil

//torna a classe nao instanciavel (abstrata)
- (id)init {
	[self doesNotRecognizeSelector:_cmd];
	[self release];
	return nil;
}

+(NSString*) strZero:(NSString*)valor tamanho:(NSInteger)tamanho {
	if ([valor length] >= tamanho){
		return valor;
	}
	while ([valor length] < tamanho) {
		valor = [NSString stringWithFormat:@"0%@", valor];
	}
	return valor;
}

+(NSString*) calculaDVCampo:(NSString*)campo {
    int multiplicador = 2;
    int multiplicacao = 0;
    int somaCampo = 0;
    
    for (int i = [campo length]; i>0; i--) {
        multiplicacao =  [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(i-1, 1)]] intValue] * multiplicador;
        if (multiplicacao >= 10) {
            NSString *multiplicacaoStr = [NSString stringWithFormat:@"%d", multiplicacao];
            int mp1 = [[NSDecimalNumber decimalNumberWithString:[multiplicacaoStr substringWithRange:NSMakeRange(0, 1)]] intValue];
            int mp2 = [[NSDecimalNumber decimalNumberWithString:[multiplicacaoStr substringFromIndex:1]] intValue];
            multiplicacao = (mp1 + mp2);
        }
        somaCampo = somaCampo + multiplicacao;
        multiplicador = (multiplicador % 2) + 1;
    }
    
    int dac = 10 - (somaCampo % 10);
    if (dac == 10) {
        dac = 0;
    }
   
    return [NSString stringWithFormat:@"%d", dac];
}

+(NSString*) calculaDVCodigoBarras:(NSString*)campo {
    int multiplicador = 4;
    int multiplicacao = 0;
    int somaCampo = 0;
    
    for (int i = 0; i < [campo length]; i++) {
        multiplicacao =  [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(i, 1)]] intValue] * multiplicador;
        somaCampo = somaCampo + multiplicacao;
        multiplicador = ((multiplicador + 5) % 8) + 2;
    }
    
    int dac = 11 - (somaCampo % 11);
    if ((dac == 0) || (dac == 1) || (dac > 9)) {
        dac = 1;
    }
    
    return [NSString stringWithFormat:@"%d", dac];
}

+(NSString*) calculaDVNossoNumero:(NSString*)campo {
     if([campo length] == 15) {
        int dig = [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(0, 1)]] intValue] * 8;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(1, 1)]] intValue] * 7;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(2, 1)]] intValue] * 6;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(3, 1)]] intValue] * 5;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(4, 1)]] intValue] * 4;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(5, 1)]] intValue] * 3;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(6, 1)]] intValue] * 2;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(7, 1)]] intValue] * 9;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(8, 1)]] intValue] * 8;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(9, 1)]] intValue] * 7;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(10, 1)]] intValue] * 6;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(11, 1)]] intValue] * 5;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(12, 1)]] intValue] * 4;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(13, 1)]] intValue] * 3;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(14, 1)]] intValue] * 2;
        
        dig = 11 - (dig % 11);
        if ((dig == 0) || (dig > 9)) {
            return [NSString stringWithString:@"0"];
        }
        return [NSString stringWithFormat:@"%d", dig];
    }else{
        int dig = [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(0, 1)]] intValue] * 3;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(1, 1)]] intValue] * 2;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(2, 1)]] intValue] * 9;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(3, 1)]] intValue] * 8;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(4, 1)]] intValue] * 7;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(5, 1)]] intValue] * 6;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(6, 1)]] intValue] * 5;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(7, 1)]] intValue] * 4;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(8, 1)]] intValue] * 3;
        dig += [[NSDecimalNumber decimalNumberWithString:[campo substringWithRange:NSMakeRange(9, 1)]] intValue] * 2;
        dig = dig % 11;
        
        if ((dig == 0) || (dig == 1)) {
            return [NSString stringWithString:@"0"];
        } 
        return [NSString stringWithFormat:@"%d", (11 - dig)];
    }
}

@end
